//Menu structure
#include "Temperature_logger.h"

volatile Main_State Curr_State=Initial_State;
volatile Main_State Prev_State=Invalid_State;
volatile Main_State Next_State=Initial_State;

volatile LCD_State Curr_LCD_State=LCD_Initial_State;
volatile LCD_State Next_LCD_State=LCD_Initial_State;
volatile signed int Index_Data=1;

volatile unsigned char Latest_Logs_Sorted[10] = {255,255,255,255,255,255,255,255,255,255};

//volatile unsigned long int Int_Counter;

extern unsigned char Latest_Logs[10]; 	//
extern char Latest_Logs_Pointer;		//
extern char First_Log_Flag;

extern unsigned char PRESENT_KEY;
extern unsigned int KEY_PRESS_DURATION_COUNT;

//char Time[8];
char Hour[6] = "00";
char Minute[6] = "00";
char Second[6] = "00";
char Day[6] = "00";
char Month[6]= "00";
char Year[6]= "00";
char Temperature;
char Temp_String[7];
char Press_UD_String[]="L-EXIT R-REDO U-PRV D-NXT";
char HH_Set[]="HH";
char MM_Set[]="MM";
char SS_Set[]="SS";
char ON[]="On";
char OFF[]="Off";
char MIN[]="MIN";
char CEL[]="*C";
char ID[]=" ";
char Blank[]="  ";
char Date[]="&Date";
char Time[]="Time";
char Colon[]=":";
char DASH[] ="-";
char ATRATE[]="@";
char Dot[]=".";
char NODATA[]="NO LOG";




static unsigned char Measure_Int_Minute=5;
static unsigned char Measure_Int_Hour=0;
volatile unsigned long int Measure_Int_In_Seconds=300;
volatile signed char Auto_Power_Off_Val = 30;
volatile Switch_State Brightness_Set_Val = Off_Sw;
volatile Switch_State Power_Save_Val = On_Sw;
volatile Switch_State Switch_Log_Temp = Off_Sw;
volatile unsigned int Brightness_Light_Level = 1;
volatile char Update_Measure_Int_Flag=0;
volatile Switch_State Auto_Power_Off_Switch = Off_Sw;

extern volatile VALID_KEY_PRESSED;

extern volatile unsigned char gSecond;
extern volatile  unsigned char gMinute;
extern volatile  unsigned char gHour;
extern volatile  unsigned char gDay;
extern volatile  unsigned char gMonth;
extern volatile  unsigned char gYear;
extern unsigned char Auto_Power_Off_Counter;

char Min_Max_Data;
Data_Ptr Data_Page;
unsigned int Curr_Page;

void LCD_Menu_Control(unsigned char Button, Press_State Press_Type, Main_State Curr_State)
{ 
char count =0;
char Temp_Pointer = Latest_Logs_Pointer - 1;


for(count=0; count<10; count++)
{
	if(Temp_Pointer==0)
	{
		Latest_Logs_Sorted[count] = Latest_Logs[Temp_Pointer];
		Temp_Pointer = 10;
	}
	else
	{
		Latest_Logs_Sorted[count] = Latest_Logs[Temp_Pointer];
	}
	Temp_Pointer--;
}


 if(Curr_State==Initial_State)
	{
	switch (Curr_LCD_State)
	{
		case LCD_Initial_State:
			if (Button != KEY_INVALID)
			{
				Next_LCD_State=LCD_Disp_Curr_Temp;
				break;
			}
			else
			{
				Next_LCD_State=Curr_LCD_State;
				break;
			}
		break;

		case LCD_Disp_Min_Temp :
			if (Button == KEY_PUSH)
			{
				Next_LCD_State=LCD_Initial_State;
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_LCD_State=LCD_Disp_Min_Temp;
				break;
			}
			else if (Button == KEY_RIGHT)
			{
				Next_LCD_State=LCD_Disp_Curr_Temp;
				break;
			}			
			else
				Next_LCD_State= Curr_LCD_State;
		break;			
		
		case LCD_Disp_Max_Temp :
			if (Button == KEY_PUSH)
			{
				Next_LCD_State=LCD_Initial_State;
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_LCD_State=LCD_Disp_Curr_Temp;
				break;
			}
			else if (Button == KEY_RIGHT)
			{
				Next_LCD_State=LCD_Disp_Max_Temp;
				break;
			}			
			else
				Next_LCD_State= Curr_LCD_State;		
		break;
		
		case LCD_Disp_Prev_Temp_Log :
			if (Button == KEY_PUSH)
			{
				Next_LCD_State=LCD_Disp_Curr_Temp;
				break;
			}
			else if (Button == KEY_UP)
			{
				Next_LCD_State=LCD_Disp_Prev_Temp_Log;
				if (Index_Data<=1)
					{Index_Data=1;}
				else
				{
					if (Press_Type==Long_Press)
					{
					Index_Data-=KEY_PRESS_DURATION_COUNT/200;
					}
					Index_Data--;
				}
				break;
			}
			else if (Button == KEY_DOWN)
			{
				Next_LCD_State=LCD_Disp_Next_Temp_Log;
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_LCD_State=LCD_Disp_Min_Temp;
				break;
			}
			else if (Button == KEY_RIGHT)
			{
				Next_LCD_State=LCD_Disp_Max_Temp;
				break;
			}			
			else
				Next_LCD_State= Curr_LCD_State;		
		break;
		
		case LCD_Disp_Next_Temp_Log :		// The state flow is same as the Prev state...do we need optimization???
			
			if (Index_Data>=10)//
				Index_Data = 10;
			
			if (Button == KEY_PUSH)
			{
				Next_LCD_State=LCD_Disp_Curr_Temp;
				break;
			}
			else if (Button == KEY_UP)
			{
				Next_LCD_State=LCD_Disp_Prev_Temp_Log;
				break;
			}
			else if (Button == KEY_DOWN)
			{
				Next_LCD_State=LCD_Disp_Next_Temp_Log;
				if (Index_Data>=10)//
				{
					{Index_Data=10;}	//	//Hold here
				}
				else 
				{
					if (Press_Type==Long_Press)
					{
						Index_Data+=KEY_PRESS_DURATION_COUNT/200;
					}
					Index_Data++;
				}
				break;
			}
			//
			//
			else if (Button == KEY_LEFT)
			{
				Next_LCD_State=LCD_Disp_Min_Temp;
				break;
			}
			else if (Button == KEY_RIGHT)
			{
				Next_LCD_State=LCD_Disp_Max_Temp;
				break;
			}			
			else
				Next_LCD_State= Curr_LCD_State;	
			
			if (Index_Data>=10)//
				Index_Data = 10;
		
		break;
		
		case LCD_Disp_Curr_Temp :
			if ((Button == KEY_PUSH) && (Press_Type == Long_Press))
			{
				Next_LCD_State=LCD_Menu_Jmp_Out_State;
				break;
			}
			else if (Button == KEY_PUSH)
			{
				Next_LCD_State=LCD_Initial_State;
				break;
			}
			else if (Button == KEY_UP)
			{
				Next_LCD_State=LCD_Disp_Prev_Temp_Log;
				break;
			}
			else if (Button == KEY_DOWN)
			{
				Next_LCD_State=LCD_Disp_Next_Temp_Log;
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_LCD_State=LCD_Disp_Min_Temp;
				break;
			}
			else if (Button == KEY_RIGHT)
			{
				Next_LCD_State=LCD_Disp_Max_Temp;
				break;
			}			
			else
				{
	
				Next_LCD_State= Curr_LCD_State;
				}
		break;
		
		case LCD_Menu_Jmp_Out_State :			// This state is where you must now go to configure setting menu.
			
			{
				//delay_ds(5);
				Next_LCD_State=LCD_Initial_State;
				break;
			}
			
		default :
			Next_LCD_State= Curr_LCD_State;
			break;
	}
  }

}	

void Main_Menu_Control(unsigned char Button, Press_State Press_Type, LCD_State Curr_LCD_State)
{
 char String_Lcd[6],Data_Tx;
 char Counter=0;
 int i,j=0;
 
 if(Curr_LCD_State == LCD_Initial_State)
 {
   switch (Curr_State)
   {
		case Initial_State :
			if ((PRESENT_KEY == KEY_PUSH) && (Press_Type == Long_Press)) 
			{ 	
				
				Next_State = Temp_Logging;
				
				break;
			}
			else 
	
			Next_State = Curr_State;		
		break;
		
		case Temp_Logging :
			if (Button == KEY_DOWN)
				{
					Next_State = Configure_Setting;
					break;
				}
			else if (Button == KEY_UP)
				{
					Next_State = PC_Transfer;
					
					break;
				}
			else if(Button == KEY_LEFT)			//----> EXIT MENU CONDITION...!!!
			{
				Next_State = Initial_State;
		
				break;
			}
			else if (Button == KEY_RIGHT)
			{
				Temp_Logging_Process();
						
				Next_State=Temp_Logging;
			}
			else
				Next_State = Curr_State;
				
		break;	

		case PC_Transfer :
			if (Button == KEY_DOWN)
				{
					Next_State = Temp_Logging;
					break;
				}
			else if (Button == KEY_UP)
				{
					Next_State = Configure_Setting;
					
					break;
				}
			else if(Button == KEY_LEFT)			//----> EXIT MENU CONDITION...!!!
			{
				Next_State = Initial_State;
		
				break;
			}
			else if(Button == KEY_RIGHT)			
			{
				//SAVE PRESENT PAGE DATA -- The DFLASH main memory must be first updated from the SRAM buffer here.
				Cont_Flash_Read_Enable(0,14); // LOC  14, 15 contain Page No
				Data_Page.Page_No_Msb = DF_SPI_RW(0);
				Data_Page.Page_No_Lsb = DF_SPI_RW(0);
				DF_CS_inactive;		
	 
				Curr_Page = (Data_Page.Page_No_Msb<<8) + (Data_Page.Page_No_Lsb);
				Buffer_To_Page(1,Curr_Page);
				DF_CS_inactive;		
					
				// HERE CALL THE USART FUNCTION FOR ACTUAL TRANSFER
				
				//USART SPEC: 19200Baudrate, 8bits, NO PARITY, 1 STOP BIT, NOFLOWCONTROL
				USARTinit();	//Careful.. This file changes the pinchangeintr mask and enables interruts..Joystick maynot work!
				
				for (i=1;i<=Curr_Page;i++)
				{
				Cont_Flash_Read_Enable(i,0);
				Counter=0;
				for(j=0;j<264;j++)
				{
					
					if ((Counter==0) || (j==0))
					{
					  Data_Tx=DF_SPI_RW(0x00);
					if((Curr_Page==1)&&(j==0)&&(Data_Tx==255))
					 {sendString(NODATA);
					 }
					 else
					 {
					 if (Data_Tx!=255)
					 {
					 itoa(Data_Tx,&String_Lcd[0],10);
					 sendString(String_Lcd); // TEMP
					 sendString(CEL);
					 sendString(ATRATE);
					 }
					 }
					 Counter++;
					}
					else if(Counter==1)
					{
					Data_Tx=DF_SPI_RW(0x00);
					if (Data_Tx!=255)
					{
					 sendString(Time);	
					 //sendString(Blank);
					 itoa(Data_Tx,&String_Lcd[0],10);
					 sendString(String_Lcd);	//HH
					 sendString(Colon);
					}
					 Counter++;
					 }
					 else if(Counter==2)
					 {
					 Data_Tx=DF_SPI_RW(0x00);
					 if (Data_Tx!=255)
					{
					 itoa(Data_Tx,&String_Lcd[0],10);
					 sendString(String_Lcd);	//MM
					 //sendString(Blank);
					}
					Counter++;					 
					 }
					 else if (Counter==3)
					 {
					 Data_Tx=DF_SPI_RW(0x00);
					if (Data_Tx!=255)
					{
					  sendString(Date);
					  //sendString(Blank);
					  itoa(Data_Tx,&String_Lcd[0],10);
					  sendString(String_Lcd);	//DD
					  sendString(Dot);
					 }
					  Counter++;
					  }
					  else if (Counter==4)
					  {
					  Data_Tx=DF_SPI_RW(0x00);
					if (Data_Tx!=255)
					{
					  itoa(Data_Tx,&String_Lcd[0],10);
					  sendString(String_Lcd);	//MM
					  sendString(Dot);
					 } 
					  Counter++;
					  }
					  else if (Counter==5)
					  {
					  Data_Tx=DF_SPI_RW(0x00);
					if (Data_Tx!=255)
					{
					  itoa(Data_Tx,&String_Lcd[0],10);
					  sendString(String_Lcd);	//YY
					  sendString(Blank);
					  sendString(DASH);
					  sendString(DASH);
					  sendString(Blank);
					}
					 Counter++;
					  }
					  else if (Counter==6)
					  {
					   Counter=0;
					   }
	
				}
				DF_CS_inactive;
			}
			//Switchoff USART and restore as before..
			USARTdisable();
			Next_State = Curr_State;
			break;
			}
		else
			Next_State = Curr_State;
				
		break;	
		
		case Configure_Setting :
			if (Button == KEY_DOWN)
			{
					Next_State = PC_Transfer;
					break;
			}
			else if (Button == KEY_UP)
			{
					Next_State = Temp_Logging;
					
					break;
			}
			else if (Button == KEY_RIGHT)
			{
				Next_State = Set_Time;
				
				break;
			}
			else if(Button == KEY_LEFT)			//----> EXIT MENU CONDITION...!!!
			{
				Next_State = Initial_State;
		
				break;
			}
			else
	
				Next_State = Curr_State;
							
		break;
		
		case Set_Time :
			if (Button == KEY_RIGHT)
			{

				Next_State = Set_Clock;
		
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_State = Configure_Setting;
		
				break;
			}
			else if (Button == KEY_DOWN)
			{
				Next_State = Measure_Interval;
			
				break;
			}
			else if (Button == KEY_UP)
			{
				Next_State = Reset_Factory_Settings;
			
				break;
			}
			else
				Next_State = Curr_State;
		break;
		
		case Set_Clock :
			if (Button == KEY_RIGHT)
			{
				Set_Clk_Process();
				Next_State = Set_Clock;
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_State = Set_Time;
				
				break;
			}
			else if ((Button == KEY_DOWN) || (Button == KEY_UP))
			{
				Next_State = Set_Date;
				
				break;
			}
			else
				Next_State = Curr_State;
		break;
		
		case Set_Date :

			if (Button == KEY_RIGHT)
			{
				Set_Date_Process();
				Next_State = Set_Date;
			
			}
			else if ((Button == KEY_DOWN)||(Button == KEY_UP))
			{
				Next_State = Set_Clock;		// Goes to Set_Clock state
				
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_State = Set_Time;			
		
				break;
			}
			else
				Next_State = Curr_State;
		break;

		case Measure_Interval :
			if (Button == KEY_RIGHT)
			{
				Measure_Int_Process();
				Next_State = Measure_Interval;
			
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_State = Configure_Setting;
		
				break;
			}
			else if (Button == KEY_DOWN)
			{
				Next_State = Auto_Power_Off;		
			
				break;				
			}
			else if (Button == KEY_UP)
			{
				Next_State = Set_Time;
	
				break;				
			}
			else
				Next_State = Curr_State;
		break;			
		
		case Auto_Power_Off :
			if (Button == KEY_RIGHT)
			{
				Auto_Power_Off_Process();
				Next_State = Auto_Power_Off;
			
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_State = Configure_Setting;
		
				break;
			}
			else if (Button == KEY_DOWN)
			{
				Next_State = Brightness_Set;		
			
				break;				
			}
			else if (Button == KEY_UP)
			{
				Next_State = Measure_Interval;
	
				break;				
			}
			else
				Next_State = Curr_State;
		break;			
	
		case Brightness_Set :
			if (Button == KEY_RIGHT)
			{
				Brightness_Set_Process();
				Next_State = Brightness_Set;
			
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_State = Configure_Setting;
		
				break;
			}
			else if (Button == KEY_DOWN)
			{
				Next_State = Reset_Factory_Settings;		
			
				break;				
			}
			else if (Button == KEY_UP)
			{
				Next_State = Auto_Power_Off;
	
				break;				
			}
			else
				Next_State = Curr_State;
		break;				

	/*	case Power_Save_Mode :
			if (Button == KEY_RIGHT)
			{
				Power_Save_Mode_Process();
				Next_State = Power_Save_Mode;
			
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_State = Configure_Setting;
		
				break;
			}
			else if (Button == KEY_DOWN)
			{
				Next_State = Reset_Factory_Settings;		
			
				break;				
			}
			else if (Button == KEY_UP)
			{
				Next_State = Brightness_Set;
	
				break;				
			}
			else
				Next_State = Curr_State;
		break;	
	*/
	case Reset_Factory_Settings :
			if (Button == KEY_RIGHT) 
			{
				Reset_Factory_Settings_Process();
				Next_State = Reset_Factory_Settings;
			
				break;
			}
			else if (Button == KEY_LEFT)
			{
				Next_State = Configure_Setting;
		
				break;
			}
			else if (Button == KEY_DOWN)
			{
				Next_State = Set_Time;		
			
				break;				
			}
			else if (Button == KEY_UP)
			{
				Next_State = Brightness_Set;
	
				break;				
			}
			else
				Next_State = Curr_State;
		break;								
		default :
			Next_State = Curr_State;
			break;
		
	} //End of switch
  }
	//VALID_KEY_PRESSED=KEY_INVALID;	
}

void Menu_Process(Main_State State_Now, LCD_State Curr_LCD_State)
{
if(Curr_LCD_State == LCD_Initial_State)
 {
	switch (State_Now)
	{	case Invalid_State :	
			LCD_TEXT("");
			
			break;
		case PC_Transfer :
			LCD_TEXT("TO PC");
			break;
		case Temp_Logging :
			LCD_TEXT("LOG");
			break;
		case Configure_Setting :
			LCD_TEXT("Config");
			break;
		case Set_Time :
			LCD_TEXT("TIMe");
			break;				
		case Set_Clock :
			LCD_TEXT("Clk");
			break;	
		case Set_Date :
			LCD_TEXT("Date");
			break;			
		case Measure_Interval :
			LCD_TEXT("Meas int");
			break;		
		case Auto_Power_Off :
			LCD_TEXT("AutoOff");
			break;				
		case Brightness_Set :
			LCD_TEXT("Brtmode");
			break;		
		case Power_Save_Mode :
			LCD_TEXT("PwrSav");
			break;		
		case Reset_Factory_Settings :
			LCD_TEXT("Reset");
			break;		
		default :
			
			break;
	}//End of switch
 //	VALID_KEY_PRESSED=KEY_INVALID;	
  }
}

void LCD_Process (LCD_State State_LCD_Now, Main_State Curr_State)
{ 

	char Temp_Value=0;
 if(Curr_State==Initial_State)
 {
	switch (State_LCD_Now)
	{
		case LCD_Disp_Curr_Temp :
			//LCD_Init();
			Temperature=getTemperature();
			itoa(Temperature,&Temp_String[0],10);
			strcat(Temp_String,CEL);
			//itoa((int)Int_Counter,&Temp_String[0],10);
			LCD_TEXT(Temp_String);
		break;
		case LCD_Initial_State :
			LCD_TEXT(" ");
			LCD_Clear;
			//LCD_Deactivate();
			
		break;
		case LCD_Disp_Min_Temp :
			LCD_TEXT(MIN)
			Cont_Flash_Read_Enable(0,6);
			Min_Max_Data = DF_SPI_RW(0);
			DF_CS_inactive;	
			delay_ds(1);
			*Temp_String="\0";
			LCD_Clear;
			itoa(Min_Max_Data, &Temp_String[0],10);
			strcat(Temp_String,CEL);
			LCD_TEXT(Temp_String);
			delay_ds(1);
		break;
		case LCD_Disp_Max_Temp :
			LCD_TEXT("MAX")
			Cont_Flash_Read_Enable(0,0);
			Min_Max_Data = DF_SPI_RW(0);
			DF_CS_inactive;
			delay_ds(1);
			*Temp_String="\0";
			LCD_Clear;
			itoa(Min_Max_Data, &Temp_String[0],10);
			strcat(Temp_String,CEL);
			LCD_TEXT(Temp_String);
			delay_ds(1);
		break;
		case LCD_Disp_Prev_Temp_Log :
		
			
			//LCD_TEXT("PRVLOG")
			
			if(Index_Data<=1)
			{
			
				//1st 4 locations of each page (e.g. for index0 to index3, index44 to index47 will be reserved)
				itoa(Index_Data, &Temp_String[0],10);
				strcat(Temp_String,ID);	
				LCD_TEXT(Temp_String);
				
				 delay_ds(1);
				*Temp_String="\0";
				LCD_Clear;
				//Temp_Value=Read_From_DFlash(1);
				
				Temp_Value = Latest_Logs_Sorted[Index_Data-1];
				
				if ((Temp_Value==255) || (First_Log_Flag==0))
				{
					LCD_TEXT(DASH);
				}
				else
				{
					itoa(Temp_Value, &Temp_String[0],10);
					strcat(Temp_String,CEL);
					LCD_TEXT(Temp_String);
				}
				//AMARTYA delay_ds(1);
				*Temp_String="\0";
				LCD_Clear;
				
			}
			else 
			{
				//Index_Data--;
				itoa(Index_Data, &Temp_String[0],10);
				strcat(Temp_String,ID);		
				LCD_TEXT(Temp_String);
				//LCD_putc((char)Index_Data);
				 delay_ds(1);
				*Temp_String="\0";
				LCD_Clear;
				//Temp_Value=Read_From_DFlash(Index_Data);
				
				Temp_Value = Latest_Logs_Sorted[Index_Data-1];
				
				if ((Temp_Value==255) || (First_Log_Flag==0))
				{
				LCD_TEXT(DASH);
				}
				else
				{
				itoa(Temp_Value, &Temp_String[0],10);
				strcat(Temp_String,CEL);
				LCD_TEXT(Temp_String);
				}
				//AMARTYA delay_ds(1);
				*Temp_String="\0";
				LCD_Clear;
			} 
		break;
		case LCD_Disp_Next_Temp_Log :
			//LCD_TEXT("NXTLOG")
			if(Index_Data>=10)		//Actually need to be calculated from the Data pointer in flash value
			{
				
				itoa(Index_Data, &Temp_String[0],10);
				strcat(Temp_String,ID);	
				LCD_TEXT(Temp_String);
				 delay_ds(1);
				*Temp_String="\0";
				LCD_Clear;
				//Temp_Value=Read_From_DFlash(10);//
				
				Temp_Value = Latest_Logs_Sorted[Index_Data-1];
				
				if ((Temp_Value==255) || (First_Log_Flag==0))
				{
				LCD_TEXT(DASH);
				}
				else
				{
				itoa(Temp_Value, &Temp_String[0],10);
				strcat(Temp_String,CEL);
				LCD_TEXT(Temp_String);
				}
				//AMARTYA delay_ds(1);
				*Temp_String="\0";
				LCD_Clear;
				
			}
			else 
			{
				//Index_Data++;
				itoa(Index_Data, &Temp_String[0],10);
				strcat(Temp_String,ID);	
				LCD_TEXT(Temp_String);
				 delay_ds(1);
				*Temp_String="\0";
				LCD_Clear;
				//Temp_Value=Read_From_DFlash(Index_Data);
				
				Temp_Value = Latest_Logs_Sorted[Index_Data-1];
				
				if ((Temp_Value==255) || (First_Log_Flag==0))
				{
				LCD_TEXT(DASH);
				}
				else
				{
				itoa(Temp_Value, &Temp_String[0],10);
				strcat(Temp_String,CEL);
				LCD_TEXT(Temp_String);
				}
				//AMARTYA delay_ds(1);
				*Temp_String="\0";
				LCD_Clear;
			}
		break;
		case LCD_Menu_Jmp_Out_State :
			
		break;
		default :
		break;
	}
  }
}

void Set_Clk_Process(void)
{	
	VALID_KEY_PRESSED = KEY_INVALID;
	LCD_Clear();
	delay_ds(1);

	/* Save Program Space..Do away with this..AMARTYA
 while (VALID_KEY_PRESSED != KEY_RIGHT)
	{
	LCD_TEXT("HH")
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
	LCD_NUMBER(gHour);
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
	LCD_TEXT("MM")
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
	LCD_NUMBER(gMinute);
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
	LCD_TEXT("SS");
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
	LCD_NUMBER(gSecond);
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
	}
*/
	LCD_TEXT(HH_Set);	
	delay_ds(1);
	while (VALID_KEY_PRESSED != KEY_RIGHT)
	{delay_ds(1);
	}
	   LCD_Clear();
	VALID_KEY_PRESSED = KEY_INVALID;		
	do
	{   
		itoa(gHour,&Hour[0],10);
		LCD_puts(Hour);
//		delay_ds(1);
		if (PRESENT_KEY==KEY_UP)
		{		
		if (KEY_PRESS_DURATION_COUNT<10)
		gHour++;
		else
		gHour+=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gHour,&Hour[0],10);
		LCD_puts(Hour);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
		if (PRESENT_KEY==KEY_DOWN)
		{
		if (KEY_PRESS_DURATION_COUNT<10)
		gHour--;
		else
		gHour-=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gHour,&Hour[0],10);
		LCD_puts(Hour);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
	}while (VALID_KEY_PRESSED!= KEY_PUSH);

	LCD_TEXT(MM_Set);	
	delay_ds(1);	
	while (VALID_KEY_PRESSED != KEY_RIGHT)
	{delay_ds(1);
	};
	LCD_Clear();
	VALID_KEY_PRESSED = KEY_INVALID;		
	do
	{	
		itoa(gMinute,&Minute[0],10);
		LCD_puts(Minute);
		//delay_ds(1);
		if (PRESENT_KEY==KEY_UP)
		{
		if (KEY_PRESS_DURATION_COUNT<10)
		gMinute++;
		else
		gMinute+=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gMinute,&Minute[0],10);
		LCD_puts(Minute);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
		if (PRESENT_KEY==KEY_DOWN)
		{
		if (KEY_PRESS_DURATION_COUNT<10)
		gMinute--;
		else
		gMinute-=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gMinute,&Minute[0],10);
		LCD_puts(Minute);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
	}while (VALID_KEY_PRESSED!= KEY_PUSH);
	
	LCD_TEXT(SS_Set);	
	delay_ds(1);
	while (VALID_KEY_PRESSED != KEY_RIGHT)
	{delay_ds(1);
	};
	   LCD_Clear();
	 VALID_KEY_PRESSED = KEY_INVALID;		
	do
	{	
		itoa(gSecond,&Second[0],10);
		LCD_puts(Second);
		
		if (PRESENT_KEY==KEY_UP)
		{
		if (KEY_PRESS_DURATION_COUNT<10)
		gSecond++;
		else
		gSecond+=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gSecond,&Second[0],10);
		LCD_puts(Second);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
		if (PRESENT_KEY==KEY_DOWN)
		{
		if (KEY_PRESS_DURATION_COUNT<10)
		gSecond--;
		else
		gSecond-=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gSecond,&Second[0],10);
		LCD_puts(Second);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
	}while (VALID_KEY_PRESSED!= KEY_PUSH);
	 LCD_Clear();
	 LCD_TEXT(Press_UD_String);
}

void Set_Date_Process(void)
{
	VALID_KEY_PRESSED = KEY_INVALID;
	LCD_Clear();
	delay_ds(1);

/* Save Program Space..Do away with this..AMARTYA
 while (VALID_KEY_PRESSED != KEY_RIGHT)
{
	LCD_TEXT("DD");
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
	LCD_NUMBER(gDay);
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
	LCD_TEXT("MM");
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
	LCD_NUMBER(gMonth);
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
	LCD_TEXT("YY");
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break; 
	delay_ds(2);
	LCD_NUMBER(gYear);
	if(VALID_KEY_PRESSED == KEY_RIGHT)
	break;
	delay_ds(2);
}
*/
	LCD_TEXT("DD");	
	delay_ds(1);
	while (VALID_KEY_PRESSED != KEY_RIGHT)
	{delay_ds(1);
	}
	   LCD_Clear();
  VALID_KEY_PRESSED = KEY_INVALID;		
	do
	{   
		itoa(gDay,&Day[0],10);
		LCD_puts(Day);

		if(PRESENT_KEY==KEY_UP)
		{
		//gDay++;
		if (KEY_PRESS_DURATION_COUNT<10)
		gDay++;
		else
		gDay+=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gDay,&Day[0],10);
		LCD_puts(Day);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}

		if(PRESENT_KEY==KEY_DOWN)
		{
		if (KEY_PRESS_DURATION_COUNT<10)
		gDay--;
		else
		gDay-=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gDay,&Day[0],10);
		LCD_puts(Day);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
	}while (VALID_KEY_PRESSED!= KEY_PUSH);

	LCD_TEXT(MM_Set);	
	delay_ds(1);	
	while (VALID_KEY_PRESSED != KEY_RIGHT)
	{delay_ds(1);
	};
	   LCD_Clear();
	 VALID_KEY_PRESSED = KEY_INVALID;		
	do
	{	
		itoa(gMonth,&Month[0],10);
		LCD_puts(Month);
		//delay_ds(1);
		if (PRESENT_KEY==KEY_UP)
		{
		if (KEY_PRESS_DURATION_COUNT<10)
		gMonth++;
		else
		gMonth+=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gMonth,&Month[0],10);
		LCD_puts(Month);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
		if (PRESENT_KEY==KEY_DOWN)
		{
		if (KEY_PRESS_DURATION_COUNT<10)
		gMonth--;
		else
		gMonth-=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gMonth,&Month[0],10);
		LCD_puts(Month);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
	}while (VALID_KEY_PRESSED!= KEY_PUSH);
	
	LCD_TEXT("YY");	
	delay_ds(1);
	while (VALID_KEY_PRESSED != KEY_RIGHT)
	{delay_ds(1);
	};
	   LCD_Clear();
	 VALID_KEY_PRESSED = KEY_INVALID;		
	do
	{	
		itoa(gYear,&Year[0],10);
		LCD_puts(Year);
		//delay_ds(1);
		if (PRESENT_KEY==KEY_UP)
		{
		if (KEY_PRESS_DURATION_COUNT<10)
		gYear++;
		else
		gYear+=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gYear,&Year[0],10);
		LCD_puts(Year);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
		if (PRESENT_KEY==KEY_DOWN)
		{
		if (KEY_PRESS_DURATION_COUNT<10)
		gYear--;
		else
		gYear-=KEY_PRESS_DURATION_COUNT/8;
		Adjust_Time();
		itoa(gYear,&Year[0],10);
		LCD_puts(Year);
		VALID_KEY_PRESSED=KEY_INVALID;
		//delay_ds(1);
		}
	}while (VALID_KEY_PRESSED!= KEY_PUSH);
	 LCD_Clear();
	 LCD_TEXT(Press_UD_String);
}

void Measure_Int_Process(void)
{
	VALID_KEY_PRESSED = KEY_INVALID;
	LCD_Clear();
	delay_ds(1);
	LCD_TEXT(HH_Set);	
	delay_ds(1);
	while (VALID_KEY_PRESSED != KEY_RIGHT) 
	{delay_ds(1);
	};
	VALID_KEY_PRESSED = KEY_INVALID;		

	do
	{	
		if (Measure_Int_Hour==255)
		Measure_Int_Hour=24;
		
		itoa(Measure_Int_Hour,&Hour[0],10);
		LCD_puts(Hour);
		
		if (PRESENT_KEY==KEY_UP)
		{
		
			if (KEY_PRESS_DURATION_COUNT<10)
				Measure_Int_Hour++;
			else
				Measure_Int_Hour+=KEY_PRESS_DURATION_COUNT/8;
				
			if (Measure_Int_Hour>24)
				Measure_Int_Hour=0;
			
			itoa(Measure_Int_Hour,&Hour[0],10);
			LCD_puts(Hour);
			VALID_KEY_PRESSED=KEY_INVALID;
			//delay_ds(1);	
		}
		
		if (PRESENT_KEY==KEY_DOWN)
		{
			if (Measure_Int_Hour==255)
				Measure_Int_Hour=24;
				
			if (KEY_PRESS_DURATION_COUNT<10)
				Measure_Int_Hour--;
			else
				Measure_Int_Hour-=KEY_PRESS_DURATION_COUNT/8;
				
			itoa(Measure_Int_Hour,&Hour[0],10);
			LCD_puts(Hour);
			VALID_KEY_PRESSED=KEY_INVALID;
			//delay_ds(1);
		}
	}while (VALID_KEY_PRESSED!= KEY_PUSH);
	
	VALID_KEY_PRESSED = KEY_INVALID;	
	
	LCD_TEXT(MM_Set);	
	delay_ds(1);

	while (VALID_KEY_PRESSED != KEY_RIGHT) 
	{delay_ds(1);
	};
	VALID_KEY_PRESSED = KEY_INVALID;		

	do
	{	
		if (Measure_Int_Minute==255)
		Measure_Int_Minute=59;
		
		itoa(Measure_Int_Minute,&Minute[0],10);
		LCD_puts(Minute);
		
		if (PRESENT_KEY==KEY_UP)
		{
			if (KEY_PRESS_DURATION_COUNT<10)
				Measure_Int_Minute++;
			else
				Measure_Int_Minute+=KEY_PRESS_DURATION_COUNT/8;
				
			if (Measure_Int_Minute>59)
				Measure_Int_Minute=0;
				
			itoa(Measure_Int_Minute,&Minute[0],10);
			LCD_puts(Minute);
			VALID_KEY_PRESSED=KEY_INVALID;
			//delay_ds(1);
		}
		if (PRESENT_KEY==KEY_DOWN)
		{
			if (Measure_Int_Minute==255)
				Measure_Int_Minute=59;
				
			if (KEY_PRESS_DURATION_COUNT<10)
				Measure_Int_Minute--;
			else
				Measure_Int_Minute-=KEY_PRESS_DURATION_COUNT/8;
			
			itoa(Measure_Int_Minute,&Minute[0],10);
			LCD_puts(Minute);
			VALID_KEY_PRESSED=KEY_INVALID;
			//delay_ds(1);	
		}
		
	}while (VALID_KEY_PRESSED!= KEY_PUSH);
	
	Measure_Int_In_Seconds=(Measure_Int_Minute*60) + (Measure_Int_Hour*3600);
	Update_Measure_Int_Flag=1;
	LCD_Clear();
	 LCD_TEXT(Press_UD_String);
}	
	
void Auto_Power_Off_Process(void)
{
	VALID_KEY_PRESSED = KEY_INVALID;	
	LCD_Clear;
	delay_ds(1);							//new
	LCD_TEXT("DD");							//
	delay_ds(1);							//
	while (VALID_KEY_PRESSED != KEY_RIGHT)	//
	{delay_ds(1);							//
	};										//
	VALID_KEY_PRESSED = KEY_INVALID;
	
	//
	
	//
	
	do
	{   
		
		if(Auto_Power_Off_Val<=0)
		Auto_Power_Off_Val=0;
		if(Auto_Power_Off_Val==0)
			LCD_TEXT("OFF")
		else
		{
			itoa(Auto_Power_Off_Val,&Day[0],10);
			LCD_puts(Day);
		}
		if(Auto_Power_Off_Val>0)
		{	
			Auto_Power_Off_Switch = On_Sw;
		}

		if(PRESENT_KEY==KEY_UP)
		{
			if (KEY_PRESS_DURATION_COUNT<10)
			Auto_Power_Off_Val++;
			else
			Auto_Power_Off_Val+=KEY_PRESS_DURATION_COUNT/8;
			if (Auto_Power_Off_Val>60)
			Auto_Power_Off_Val=0;
			if(Auto_Power_Off_Val==0)
				LCD_TEXT("OFF")
			else
			{
				itoa(Auto_Power_Off_Val,&Day[0],10);
				LCD_puts(Day);
			}
			VALID_KEY_PRESSED=KEY_INVALID;
			//delay_ds(1);
			//gDay++;
			
			//Adjust_Time();
				
		}

		if(PRESENT_KEY==KEY_DOWN)
		{
			
			
			if (KEY_PRESS_DURATION_COUNT<10)
			Auto_Power_Off_Val--;
			else
			Auto_Power_Off_Val-=KEY_PRESS_DURATION_COUNT/8;
			
			if(Auto_Power_Off_Val<=0)
			Auto_Power_Off_Val=0;
			
			if(Auto_Power_Off_Val==0)
				LCD_TEXT("OFF")
			else
			{
				itoa(Auto_Power_Off_Val,&Day[0],10);
				LCD_puts(Day);
			}
			
			//Adjust_Time();
			
			VALID_KEY_PRESSED=KEY_INVALID;
			//delay_ds(1);
		}
	}while (VALID_KEY_PRESSED!= KEY_PUSH);
	Auto_Power_Off_Counter=Auto_Power_Off_Val;
	LCD_Clear();
	LCD_TEXT(Press_UD_String);
}

void Brightness_Set_Process(void)
{
	int Light=0;
	unsigned int Light_Level;
	VALID_KEY_PRESSED = KEY_INVALID;
	char Light_String[8];
	do{
	Light=getLight();
	Light_Level=1;
		while(Light<499){
		Light_Level=(Light_Level*10)+1;
		Light+=100;
		} ;
	if(Light_Level>11111)
	{
	LCD_TEXT("Too Brt");
	}
	else
	{ 
	
	itoa(Light_Level,&Light_String[0],10);
	LCD_puts(Light_String);
	}
	delay_ds(1);
	LCD_Clear();
	} while (VALID_KEY_PRESSED!=KEY_RIGHT);
	
	
	do {
	if (Brightness_Set_Val==On_Sw)
	{
	LCD_TEXT(ON)
	
	}
	else
	{
	LCD_TEXT(OFF)
	
	}
	if (PRESENT_KEY==KEY_UP)
	{
	Brightness_Set_Val=On_Sw;
	Brightness_Light_Level = Light_Level;
	VALID_KEY_PRESSED=KEY_INVALID;

	}
	if (PRESENT_KEY==KEY_DOWN)
	{
	Brightness_Set_Val=Off_Sw;
	Brightness_Light_Level = 1;
	VALID_KEY_PRESSED=KEY_INVALID;

	}
	//delay_ds(1);
	}while (VALID_KEY_PRESSED!= KEY_PUSH);
	LCD_Clear();
	LCD_TEXT(Press_UD_String);

}

/*
void Power_Save_Mode_Process(void)
{
	
	VALID_KEY_PRESSED = KEY_INVALID;	
	LCD_Clear;
	while (VALID_KEY_PRESSED != KEY_RIGHT) 
	{delay_ds(1);
	};
	VALID_KEY_PRESSED = KEY_INVALID;
	do {
	if (Power_Save_Val==On_Sw)
	LCD_TEXT(ON)
	else
	LCD_TEXT(OFF)
	if (PRESENT_KEY==KEY_UP)
	{
	Power_Save_Val=On_Sw;
	VALID_KEY_PRESSED=KEY_INVALID;
	//LCD_TEXT("ON")
	//delay_ds(1);
	}
	if (PRESENT_KEY==KEY_DOWN)
	{
	Power_Save_Val=Off_Sw;
	VALID_KEY_PRESSED=KEY_INVALID;
	//LCD_TEXT("OF")
	//delay_ds(1);
	}

	}while (VALID_KEY_PRESSED!= KEY_PUSH);
	LCD_Clear();
	LCD_TEXT(Press_UD_String);

}
*/

void Reset_Factory_Settings_Process(void)
{
	VALID_KEY_PRESSED = KEY_INVALID;	
//	LCD_Clear;
//	while (VALID_KEY_PRESSED != KEY_RIGHT) 
//	{delay_ds(1);
//	};

	//VALID_KEY_PRESSED = KEY_INVALID;	
	//do
	//{
	//LCD_TEXT("SURE");
	//delay_ds(1);
	//}while (VALID_KEY_PRESSED != KEY_RIGHT) ;
	
	VALID_KEY_PRESSED = KEY_INVALID;	
	do
	{
	//delay_ds();
	LCD_TEXT("ERASE ALL DATA")
	}while (VALID_KEY_PRESSED == KEY_INVALID) ;
	
	//RESET CALIB PROCESS...
	//ERASE MEMORY...(THIS PROCESS NEEDS TO BE WRITTEN)


	
	if (VALID_KEY_PRESSED==KEY_PUSH)
	{
	Erase_Dflash_Full();
	DF_Page0_init(); //Initialse all Max,Min values and reset data pointer.
	Measure_Int_Minute=5;
	Measure_Int_Hour=0;
	Auto_Power_Off_Val = 30;
	Brightness_Set_Val = Off_Sw;
	Power_Save_Val = On_Sw;
	Switch_Log_Temp=Off_Sw;
		
	//delay_ds(5);
	LCD_Clear;
	LCD_TEXT("DONE")
	}
	else
	LCD_TEXT("ABORT")
	delay_ds(2);
	LCD_TEXT(Press_UD_String);
}
	
void Temp_Logging_Process(void)
{
	VALID_KEY_PRESSED = KEY_INVALID;
	//LCD_Clear;
	do {
	if (Switch_Log_Temp==On_Sw)
	LCD_TEXT(ON)
	else
	LCD_TEXT(OFF)
	if (PRESENT_KEY==KEY_UP)
	{
	Switch_Log_Temp=On_Sw;
	VALID_KEY_PRESSED=KEY_INVALID;
	}
	if (PRESENT_KEY==KEY_DOWN)
	{
	Switch_Log_Temp=Off_Sw;
	VALID_KEY_PRESSED=KEY_INVALID;
	}
	}while (VALID_KEY_PRESSED!= KEY_PUSH);
	VALID_KEY_PRESSED = KEY_INVALID;
	LCD_Clear();
	LCD_TEXT(Press_UD_String);
}

